package com.css.fxfzaqbz.modules.callJcData.controller;

import com.css.fxfzaqbz.base.response.RestResponse;
import com.css.fxfzaqbz.modules.callJcData.entity.SysDataVersion;
import com.css.fxfzaqbz.modules.callJcData.param.DataVersionPageParam;
import com.css.fxfzaqbz.modules.callJcData.param.SysDataVersionPageParam;
import com.css.fxfzaqbz.modules.callJcData.param.SysDataVersionSameTypeDataExistParam;
import com.css.fxfzaqbz.modules.callJcData.param.SysDataVersionSaveMetaDataInfoParam;
import com.css.fxfzaqbz.modules.callJcData.service.GasholderService;
import com.css.fxfzaqbz.modules.callJcData.service.impl.SysDataVersionService;
import com.css.fxfzaqbz.modules.callJcData.vo.IdsVO;
import com.css.fxfzaqbz.modules.callJcData.vo.SitePhotoVO;
import com.css.fxfzaqbz.modules.callJcData.service.SiteService;
import com.css.fxfzaqbz.modules.supergraph.vo.VersionVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.List;





/*
* 版本管理的接口
*
* */
@Slf4j
@RestController
@RequestMapping("fxfzaqbz/sysDataVersion")
public class SysDataVersionController {

    @Autowired
    private SysDataVersionService sysDataVersionService;
    @Resource
    private GasholderService gasholderService;
    @Resource
    private SiteService siteService;


    /*
    * 保存新版本
    * */
    @PostMapping("saveNewVersion")
    public RestResponse saveNewVersion(@RequestBody SysDataVersion sysDataVersion) {
        try {
            return RestResponse.succeed(sysDataVersionService.saveNewVersion(sysDataVersion));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail("新增失败!");
        }
    }


    /*
    * 获取版本列表   dataType 为1时 是获取 储气罐  为2时 是获取重点场所
    * */
    @GetMapping("getVersionCodeList/{dataType}")
    public RestResponse getVersionCodeListByType(@PathVariable("dataType") String dataType) {
        return RestResponse.succeed(sysDataVersionService.getVersionCodeListByType(dataType));
    }

    /*
    *保存元数据信息
    * */
    @PostMapping("saveMetaDataInfo")
    public RestResponse saveMetaDataInfo(@RequestBody SysDataVersionSaveMetaDataInfoParam param) {
        try {
            sysDataVersionService.saveMetaDataInfo(param);
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail("保存失败!");
        }
    }


    /*
    * 根据版本号查询元数据信息
    * */
    @GetMapping("getMetaDataByVersionCode")
    public RestResponse getMetaDataByVersionCode(String versionCode,String typeCode) {
        try {
            return RestResponse.succeed(sysDataVersionService.getMetaDataByVersionCode(versionCode,typeCode));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail("查询失败!");
        }
    }


    //@Operation(summary = "覆盖旧版本")
    @PostMapping("overwriteOldVersion")
    public RestResponse overwriteOldVersion(@RequestBody SysDataVersion sysDataVersion) {
        try {
            return RestResponse.succeed(sysDataVersionService.overwriteOldVersion(sysDataVersion));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail("覆盖失败!");
        }
    }

   // @Operation(summary = "删除版本数据")
    @PostMapping("del")
    public RestResponse del(@RequestBody String id) {
        try {
            sysDataVersionService.delByIds(Collections.singletonList(id));
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail("删除失败");
        }
    }

    //@Operation(summary = "分页条件查询接口")
    @GetMapping("page")
    public RestResponse page(SysDataVersionPageParam sysDataVersionPageParam) {
        try {
            return RestResponse.succeed(sysDataVersionService.page(sysDataVersionPageParam));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail("查询失败!");
        }
    }

    //@Operation(summary = "根据条件查询数据版本列表接口（无分页）")
    @GetMapping("list")
    public RestResponse list(SysDataVersionPageParam sysDataVersionPageParam) {
        try {
            return RestResponse.succeed(sysDataVersionService.list(sysDataVersionPageParam));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail("查询失败!");
        }
    }



    /*
    安保目前不用解析Excel
    * */
    @GetMapping("parseExcel")
    public RestResponse parseExcel(String fileId) {
        try {
            return RestResponse.succeed(sysDataVersionService.parseExcel(fileId));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }


    /*
    * 解析Shp 文件
    * */
    @GetMapping("parseShp")
    public RestResponse parseShp(String fileIds,String typeCode) {
        try {
            return sysDataVersionService.parseShp(fileIds,typeCode);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    @PostMapping("dataExport")
    public RestResponse dataExport(HttpServletResponse response, @RequestBody List<String> idList) {
        try {
            sysDataVersionService.dataExport(response, idList, false);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        return RestResponse.succeed();
    }


    /*
    * 根据省市区 类型 版本 查询数据
    * */
    @PostMapping("getDataView")
    public RestResponse getDataView(@RequestBody SysDataVersionPageParam sysDataVersionPageParam) {

        return RestResponse.succeed(sysDataVersionService.getDataVersionView(sysDataVersionPageParam));

    }


    /*
     * 根据省市区 类型 版本 查询数据
     * */
    @PostMapping("getFyDataView")
    public RestResponse getFyDataView(@RequestBody VersionVO versionVO) {
        return RestResponse.succeed(sysDataVersionService.getFyDataView(versionVO));

    }




    /*
     * 根据版本号查询业务数据信息
     * */
    @GetMapping("getYwData")
    public RestResponse getYwData(String versionCode,String typeCode) {

        if (typeCode.equals("1")){
            return RestResponse.succeed(gasholderService.getYwData(versionCode));

        }
        if (typeCode.equals("2")){

            return RestResponse.succeed(siteService.getYwData(versionCode));

        }
        return RestResponse.succeed();
    }

    /*
     * 根据版本号查询业务数据信息  业务数据是分页的
     * */
    @GetMapping("getFyYwData")
    public RestResponse getFyYwData(DataVersionPageParam dataVersionPageParam) {

        if (dataVersionPageParam.getTypeCode().equals("1")){
            return gasholderService.getFyYwData(dataVersionPageParam);
        }
        if (dataVersionPageParam.getTypeCode().equals("2")){
            return siteService.getFyYwData(dataVersionPageParam);
        }
        return RestResponse.succeed();
    }



    @GetMapping("sameTypeDataExist")
    //@Operation(summary = "判断同类别数据是否已经存在")
    public RestResponse sameTypeDataExist(SysDataVersionSameTypeDataExistParam sysDataVersionSameTypeDataExistParam) {
        try {
            return RestResponse.succeed(sysDataVersionService.sameTypeDataExist(sysDataVersionSameTypeDataExistParam));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail("查询失败!");
        }
    }



    @PostMapping("uploadSitePhotos")//添加图片id
    public RestResponse uploadSitePhotos(@RequestBody SitePhotoVO sitePhotoVO) {
        try {
                siteService.uploadSitePhotos(sitePhotoVO);
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail("查询失败!");
        }
    }




   // @Operation(summary = "获取字段说明")
    @GetMapping("getFileDescByDictItemId/{dictItemId}")
    public RestResponse getFileDescByDictItemId(@PathVariable("dictItemId") String dictItemId) {
        try {
            return RestResponse.succeed(siteService.getByDictItemId(dictItemId));
            } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail("查询失败!");
        }
    }


    @PostMapping("submitYwData")
    public RestResponse submitYwData(@RequestBody IdsVO ids) {
        try {
            siteService.submitYwData(ids);
            return RestResponse.succeed("提交成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail("提交失败!");
        }
    }


}
